<!-- eslint-disable vue/multi-word-component-names -->
<template>
  <div id="bc">
    <el-form class="login-container" >
      <h3 class="title">欢迎来到宠物救济及领养平台</h3>
      <h3 class="title">请您首先登录</h3>
      
      <el-form-item>
        <el-input 
          type="text" 
          placeholder="用户账号" 
          v-model="logname" 
          autocomplete="off"
        ></el-input>
      </el-form-item>
      
      <el-form-item>
        <el-input 
          type="password" 
          placeholder="用户密码" 
          v-model="password" 
          show-password 
          autocomplete="off"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button 
          type="primary" 
          @click="doLogin" 
          style="width: 100%"
        >登录</el-button>
      </el-form-item>

      <el-link type="primary" @click="toRegister()">用户注册</el-link>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import axios from "axios";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import { useUserStore } from "@/stores/user";

// 初始化路由、状态管理和表单数据
const router = useRouter();
const userStore = useUserStore();


const logname = ref("");
const password = ref("");

// 登录处理函数
const doLogin = async () => {
  try {
    // 准备登录请求数据
    const formData = {
      account: logname.value,
      password: password.value,
    };

    // 发送登录请求
    const res = await axios.post(
      "http://114.215.191.128:8080/pet-adopt/auth/login",
      formData,
      {
        headers: {
          "Content-Type": "application/x-www-form-urlencoded",
        },
      }
    );

    // 处理登录成功逻辑
    const result = res.data;
    if (result.message === "登录成功") {
      ElMessage.success(result.message);
      
      // 保存认证信息并跳转
      const token = result.token;
      const userInfo = result.user;
      userStore.setToken(token);
      userStore.setUser(userInfo);
      
      console.log("登录成功，跳转到首页");
      router.push("/home");
    }
  } catch (error) {
    // 处理登录失败逻辑
    if (error.response) {
      if (error.response.status === 401) {
        ElMessage.error("账号或密码错误");
      } else {
        ElMessage.error("账户异常，已被禁用");
      }
    }
  }
};

// 跳转注册页面
const toRegister = () => {
  console.log("点击注册");
  router.push("/register");
};
</script>




<style lang="scss" scoped>
#bc {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  width: 100%;
  margin: 0;
  padding: 20px;
  box-sizing: border-box;
  // background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%); /* 添加背景渐变 */
  position: fixed;
  top: 0;
  left: 0;
}

.login-container {
  width: 100%;
  max-width: 350px;
  padding: 30px 35px 15px 35px;
  text-align: center;
  background: white;
  border: 1px solid #eaeaea;
  border-radius: 10px;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.08);
  transition: transform 0.3s ease;

  /* 居中微调 */
  margin: auto;
  position: relative;
}

.title {
  margin-bottom: 20px;
}
</style>